-- fixed_layer_bonuses
-- created by wangy
-- 前进到X层，所有基础属性提升N%

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 进入下层触发触发
    trigger = function(who, prop, arg)
        if who.type ~= OBJECT_TYPE_USER then
            return 0;
        end

        -- 客户端此时还在前一层，需要+1计算
        local floor = DungeonM.currentLayer() + 1;

        -- 不是在相应层/没有该属性，不处理
        if floor ~= tonumber(prop[2]) or prop[3] <= 0 then
            return 0;
        end

        local add    = prop[3];
        local transTimes = tonumber(who.dbase:query("trans_times"));

        -- 除第一次和第四次，其余增加1%
        if transTimes ~= 0 and transTimes ~= 3 then
            add = 10;
        end

        local baseAttrib = InterfaceM.calcBaseAttrib(who);
        local attack = baseAttrib["attack"];
        local magic  = baseAttrib["magic"];
        local max_hp = baseAttrib["max_hp"];
        local max_mp = baseAttrib["max_mp"];

        attack = math.max(1, math.modf(attack * add / 1000));
        magic  = math.max(1, math.modf(magic * add / 1000));
        max_hp = math.max(1, math.modf(max_hp * add / 1000));
        max_mp = math.max(1, math.modf(max_mp * add / 1000));

        local ret = { { 2, "attack", attack },
                      { 2, "magic",  magic },
                      { 2, "max_hp", max_hp },
                      { 2, "max_mp", max_mp },
        };

        -- 给奖励
        for _, bonus in pairs(ret) do
            BonusM.doBonus(bonus, "fixed_layer_bonuses");
        end

        transTimes = transTimes + 1;
        who.dbase:set("trans_times", transTimes);

        -- 触发特技发动事件, 先记在玩家身上
        ME.user.dbase:setTemp("speicial_skill_trigger", prop[1]);

        -- 根据次数进行不同的变身
        local shapeId = FormulaM.invoke("CALC_SHAPE_ID_BY_TRANS_TIMES", transTimes);

        -- 记录为变身属性
        if shapeId then
            -- 因为存在多次变身，所以先清空以前的
            PropM.deleteProp(who, "shape_change");

            -- 记录下变身状态
            PropM.record(who, { 808, shapeId, 2, -1 }, "shape_change");
            
            -- 添加需要变身的标记
            who.dbase:setTemp("play_trans_effect", 1);
        end

        -- 标记发言
        ME.user.dbase:setTemp("hero_talk", {["trans_times"] = transTimes, });

        return prop[3];
    end,

    record = function(target, prop, path)
        return false;
    end
};
